home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / MacZoop 1.1 / More Classes / ZPictWindow.cpp next >
Encoding:
C/C++ Source or Header  |  1996-05-01  |  4.3 KB  |  219 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZPictWindow.cpp        -- a window that displays PICT files
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22. #include    "ZPictWindow.h"
  23. #include    "ZErrors.h"
  24. #include    "ZDefines.h"
  25.  
  26.  
  27. /*--------------------------------***  CONSTRUCTOR  ***---------------------------------*/
  28.  
  29. ZPictWindow::ZPictWindow(ZCommander* aBoss, short windID)
  30.     : ZScroller(aBoss, windID, TRUE, TRUE )
  31. {
  32.     thePicture = NULL;
  33. }
  34.  
  35.  
  36. /*---------------------------------***  DESTRUCTOR  ***---------------------------------*/
  37.  
  38. ZPictWindow::~ZPictWindow()
  39. {
  40.     if (thePicture)
  41.         KillPicture(thePicture);
  42. }
  43.  
  44. /*--------------------------------***  DRAWCONTENT  ***---------------------------------*/
  45. /*    
  46.  
  47. overrides ZScroller to draw the picture in the window
  48.  
  49. ----------------------------------------------------------------------------------------*/
  50.  
  51. void    ZPictWindow::DrawContent()
  52. {
  53.     Rect    pFrame;
  54.     
  55.     if (thePicture)
  56.     {
  57.         pFrame = (*thePicture)->picFrame;
  58.         OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  59.         DrawPicture(thePicture, &pFrame);
  60.     }
  61. }
  62.  
  63.  
  64. /*----------------------------------***  OPENFILE  ***----------------------------------*/
  65. /*    
  66.  
  67. overrides the base ZWindow to open a PICT file on disk to the window
  68.  
  69. ----------------------------------------------------------------------------------------*/
  70.  
  71. void    ZPictWindow::OpenFile(OSType fType)
  72. {
  73.     // read the picture into the picture handle
  74.     
  75.     FInfo    fi;
  76.     short    refNum;
  77.     long    pSize;
  78.     Handle    temp = NULL;
  79.     Rect    pFrame;
  80.     
  81.     if (macFile.vRefNum != kNoFile)
  82.     {
  83.         FailOSErr(FSpGetFInfo(&macFile, &fi));    
  84.         
  85.         if (fi.fdType != 'PICT')
  86.             FailOSErr(paramErr);
  87.             
  88.         FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
  89.         
  90.         // skip the first 512 bytes of the file
  91.         
  92.         try
  93.         {
  94.             FailOSErr(SetFPos(refNum, fsFromStart,  512));
  95.             
  96.             // how big is the rest of the file?
  97.             
  98.             FailOSErr(GetEOF(refNum, &pSize));
  99.             pSize -= 512;
  100.             
  101.             if (pSize > 0)
  102.             {
  103.                 FailNIL(temp = NewHandle(pSize));
  104.             
  105.                 HLock(temp);
  106.                 FailOSErr(FSRead(refNum, &pSize, *temp));
  107.                 HUnlock(temp);
  108.             }
  109.             
  110.             FSClose(refNum);
  111.             
  112.             if (thePicture)
  113.                 KillPicture(thePicture);
  114.                 
  115.             thePicture  = (PicHandle)temp;
  116.             
  117.             // set the scroll dimensions to the picture's frame
  118.             
  119.             pFrame = (*thePicture)->picFrame;
  120.             OffsetRect(&pFrame, -pFrame.left, -pFrame.top);
  121.             SetBounds(&pFrame);
  122.             SetScrollAmount(8,8);
  123.             
  124.             pFrame.right += kStdScrollbarWidth;
  125.             pFrame.bottom += kStdScrollbarWidth;
  126.             
  127.             SetSize(pFrame.right, pFrame.bottom);
  128.         }
  129.         catch(OSErr err)
  130.         {
  131.             FSClose(refNum);
  132.             
  133.             if (temp)
  134.             {
  135.                 HUnlock(temp);
  136.                 DisposeHandle(temp);
  137.             }    
  138.             throw err;
  139.         }
  140.     }
  141.     inherited::OpenFile( fType );
  142. }
  143.  
  144.  
  145.  
  146. /*--------------------------------***  SAVEFILE  ***---------------------------------*/
  147. /*    
  148.  
  149. overrides the base ZWindow to save the image as a PICT file on disk
  150.  
  151. ----------------------------------------------------------------------------------------*/
  152.  
  153. void    ZPictWindow::SaveFile()
  154. {
  155.     // this saves the window contents as a PICT file in the file <macFile>.
  156.  
  157.     short        refNum;
  158.     long        pSize;
  159.     Handle        header = NULL;
  160.     OSErr        theErr;
  161.     
  162.     if ((macFile.vRefNum != kNoFile) && (thePicture != NULL))
  163.     {
  164.         theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
  165.         
  166.         if (theErr == fnfErr)
  167.         {
  168.             FailOSErr(FSpCreate(&macFile, gMacZoopSignature, 'PICT', 0));
  169.             FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
  170.         }
  171.         else
  172.             FailOSErr(theErr);
  173.         
  174.         try
  175.         {
  176.             // make a header and write it out. This is just 512 bytes of zeroes.
  177.             
  178.             FailNIL(header = NewHandleClear(pSize = 512));
  179.             
  180.             HLock(header);
  181.             FailOSErr(FSWrite(refNum, &pSize, *header));    
  182.             HUnlock(header);
  183.             DisposeHandle(header);
  184.             header = NULL;
  185.             
  186.             // now write out the picture data
  187.             
  188.             pSize = GetHandleSize((Handle) thePicture);
  189.             
  190.             HLock((Handle) thePicture);
  191.             FailOSErr(FSWrite(refNum, &pSize, *thePicture));
  192.             HUnlock((Handle) thePicture);
  193.             
  194.             // set the file length to the current mark
  195.             
  196.             FailOSErr(GetFPos(refNum, &pSize));
  197.             FailOSErr(SetEOF(refNum, pSize));
  198.             
  199.             FSClose(refNum);
  200.         }
  201.         catch(OSErr err)
  202.         {    
  203.             FSClose(refNum);
  204.             
  205.             if (header)
  206.             {
  207.                 HUnlock(header);
  208.                 DisposeHandle(header);
  209.             }
  210.             
  211.             HUnlock((Handle) thePicture);
  212.             
  213.             throw err;
  214.         }
  215.         inherited::SaveFile();
  216.     }
  217. }
  218.  
  219.